package com.example.hzly.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.hzly.common.Result;
import com.example.hzly.dto.RouteDTO;
import com.example.hzly.dto.RouteReviewDTO;
import com.example.hzly.entity.Route;
import com.example.hzly.entity.RouteReview;
import com.example.hzly.service.RouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/routes")
public class RouteController {

    @Autowired
    private RouteService routeService;

    /**
     * 获取路线详情
     */
    @GetMapping("/{id}")
    public Result<RouteDTO> getRouteDetail(@PathVariable Long id) {
        return Result.ok(routeService.getRouteDetail(id));
    }

    /**
     * 分页查询路线列表（支持筛选）
     */
    @GetMapping("/page")
    public Result<IPage<RouteDTO>> pageRoutes(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String theme,
            @RequestParam(required = false) String difficulty,
            @RequestParam(required = false) Integer duration,
            @RequestParam(required = false) String sort) {
        IPage<RouteDTO> pageResult = routeService.pageRoutes(page, size, theme, difficulty, duration, sort);
        return Result.ok(pageResult);
    }

    /**
     * 获取路线筛选选项
     */
    @GetMapping("/filter-options")
    public Result<Map<String, Object>> getFilterOptions() {
        Map<String, Object> filterOptions = routeService.getFilterOptions();
        return Result.ok(filterOptions);
    }

    /**
     * 获取路线列表
     */
    @GetMapping
    public Result<IPage<RouteDTO>> getRouteList(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Route> page = new Page<>(current, size);
        return Result.ok(routeService.getRouteList(page));
    }

    /**
     * 获取热门路线
     */
    @GetMapping("/hot")
    public Result<IPage<RouteDTO>> getHotRoutes(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "6") Integer size) {
        Page<Route> page = new Page<>(current, size);
        return Result.ok(routeService.getHotRoutes(page));
    }

    /**
     * 根据目的地获取路线
     */
    @GetMapping("/destination/{destinationId}")
    public Result<IPage<RouteDTO>> getRoutesByDestination(
            @PathVariable Long destinationId,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Route> page = new Page<>(current, size);
        return Result.ok(routeService.getRoutesByDestination(destinationId, page));
    }

    /**
     * 根据主题获取路线
     */
    @GetMapping("/theme/{theme}")
    public Result<IPage<RouteDTO>> getRoutesByTheme(
            @PathVariable String theme,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Route> page = new Page<>(current, size);
        return Result.ok(routeService.getRoutesByTheme(theme, page));
    }

    /**
     * 根据难度获取路线
     */
    @GetMapping("/difficulty/{difficulty}")
    public Result<IPage<RouteDTO>> getRoutesByDifficulty(
            @PathVariable String difficulty,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Route> page = new Page<>(current, size);
        return Result.ok(routeService.getRoutesByDifficulty(difficulty, page));
    }

    /**
     * 搜索路线
     */
    @GetMapping("/search")
    public Result<IPage<RouteDTO>> searchRoutes(
            @RequestParam String keyword,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<Route> page = new Page<>(current, size);
        return Result.ok(routeService.searchRoutes(keyword, page));
    }

    /**
     * 获取相关路线
     */
    @GetMapping("/related/{destinationId}")
    public Result<List<RouteDTO>> getRelatedRoutes(
            @PathVariable Long destinationId,
            @RequestParam(defaultValue = "4") Integer limit) {
        return Result.ok(routeService.getRelatedRoutes(destinationId, limit));
    }

    /**
     * 获取所有主题
     */
    @GetMapping("/themes")
    public Result<List<String>> getAllThemes() {
        return Result.ok(routeService.getAllThemes());
    }

    /**
     * 获取路线评论
     */
    @GetMapping("/{id}/reviews")
    public Result<IPage<RouteReviewDTO>> getRouteReviews(
            @PathVariable Long id,
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<RouteReview> page = new Page<>(current, size);
        return Result.ok(routeService.getRouteReviews(id, page));
    }

    /**
     * 获取路线统计信息
     */
    @GetMapping("/stats")
    public Result<Map<String, Object>> getRouteStats() {
        return Result.ok(routeService.getRouteStats());
    }

    /**
     * 增加预订次数
     */
    @PostMapping("/{id}/book")
    public Result<Void> incrementBookCount(@PathVariable Long id) {
        routeService.incrementBookCount(id);
        return Result.ok();
    }

    /**
     * 添加路线评论或回复
     */
    @PostMapping("/{id}/review")
    public Result<Void> addReview(@PathVariable Long id, @RequestBody RouteReview review) {
        review.setRouteId(id);
        routeService.addReview(review);
        return Result.ok();
    }

    /**
     * 删除评论（只能删除自己的评论）
     */
    @DeleteMapping("/review/{reviewId}")
    public Result<Void> deleteReview(@PathVariable Long reviewId, @RequestParam String name) {
        routeService.deleteReview(reviewId, name);
        return Result.ok();
    }

    /**
     * 获取嵌套评论列表
     */
    @GetMapping("/{id}/reviews/nested")
    public Result<List<RouteReviewDTO>> getNestedReviews(@PathVariable Long id) {
        return Result.ok(routeService.getNestedReviews(id));
    }
}